// get all possible matches of the spoken words
ArrayList<String> matches = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
Log.v(TAG, "Spoken: " + matches.toString());
// need all items here to check which fits best
ArrayList<Item> allItems = adapter.getAllItems();
// save the similarity to the spoken words for each item
int[] similarity = new int[allItems.size()];
// this int describes the position of the best fitting item in the arraylist
int bestChoice = -1;
for (int i = 0; i < allItems.size(); i++) {
	similarity[i] = 0;
	for (int j = 0; j < matches.size(); j++) { // check name compared to all matches
		if (similarity(matches.get(j), allItems.get(i).name) > similarity[i]) { // increase the similarity if another match fits better
			similarity[i] = (int) (similarity(matches.get(j), allItems.get(i).name) * 100);
		}
		try {
			// parse the amount found in this match as item amount
			// (inside the loop because there may be some matches without any numbers)
			allItems.get(i).amount = Double.parseDouble(matches.get(j).replaceAll("\\D", ""));
		} catch (Exception e) {
		}
	}
	// set this item to the best choice if it fits better than the previous best one
	if (bestChoice == -1 || similarity[i] > similarity[bestChoice]) {
		bestChoice = i;
	}
}

// do nothing if no item matched
if (bestChoice == -1) {
	progressD.dismiss();
	break;
}

final Item bestItem = allItems.get(bestChoice);

Log.v(TAG, "Best choice: " + bestItem.name);

// show a question whether to add the found item
AlertDialog.Builder dB = new AlertDialog.Builder(this);
dB.setTitle(getString(R.string.add));
dB.setMessage(bestItem.amount
		+ (bestItem.unit == 'm' ? getString(R.string.unit_ml) : bestItem.unit == 'l' ? getString(R.string.unit_l) : bestItem.unit == 'g' ? getString(R.string.unit_g) : bestItem.unit == 'k' ? getString(R.string.unit_kg) : getString(R.string.unit_pc))
		+ " " + bestItem.name + " ?");
dB.setPositiveButton(getString(R.string.yes), new DialogInterface.OnClickListener() {

	public void onClick(DialogInterface dialog, int id) {
		adapter.addToMainList(bestItem);
		infos.add(bestItem);
		initList();
		myAdapter.notifyDataSetChanged();
	}
});
dB.setNegativeButton(getString(R.string.no), null);
AlertDialog d = dB.create();
d.show();